package com.transcasafe.eanpay.example.pay;

import com.alibaba.fastjson.JSONObject;
import com.transcasafe.eanpay.core.config.EanPayConfig;
import com.transcasafe.eanpay.core.service.EanPayCardService;
import com.transcasafe.eanpay.core.service.model.BillToInfoInRequest;
import com.transcasafe.eanpay.core.service.model.CardInfoInRequest;
import com.transcasafe.eanpay.core.service.model.DeviceInfoInRequest;
import com.transcasafe.eanpay.core.service.model.OrderInfoInRequest;
import com.transcasafe.eanpay.core.service.model.PaymentsPayRequest;
import com.transcasafe.eanpay.core.service.model.PaymentsPayResponse;
import com.transcasafe.eanpay.core.service.model.ProductInfoInRequest;
import com.transcasafe.eanpay.core.service.model.ShipToInfoInRequest;
import com.transcasafe.eanpay.core.util.NonceUtil;
import com.transcasafe.eanpay.example.MchConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * 代收订单创建
 */
public class EanPayPaymentsExample {
    public static void main(String[] args) {
        EanPayConfig config = new EanPayConfig.Builder().mchNo(MchConfig.mchNo).mchKey(MchConfig.mchKey).build();
        EanPayCardService service = new EanPayCardService.Builder().config(config).hostname(MchConfig.hostname).build();
        PaymentsPayRequest request = new PaymentsPayRequest();
        request.setNotifyUrl("http://127.0.0.1:8080/v1/card-pay/cybersource-payments/notify-test");
        request.setPayerAuthRedirectUrl("http://localhost:8088/card-payer-auth/payment-result");
        String price = "100";
        // 卡信息
        CardInfoInRequest cardInfo = new CardInfoInRequest();
        cardInfo.setCardNumber("5200000000002235");
        cardInfo.setExpirationMonth("12");
        cardInfo.setExpirationYear("2028");
        cardInfo.setSecurityCode("999");
        request.setCardInfo(cardInfo);
        // 订单信息
        OrderInfoInRequest orderInfo = new OrderInfoInRequest();
        orderInfo.setAmount(price);
        orderInfo.setCurrency("USD");
        orderInfo.setMchOrderNo(NonceUtil.createNonce(16));
        orderInfo.setTransactionUrl("http://127.0.0.1");
        // 商品信息
        List<ProductInfoInRequest> productInfo = new ArrayList<>();
        ProductInfoInRequest productInfoInRequest = new ProductInfoInRequest();

        productInfoInRequest.setProductName("productName");
        productInfoInRequest.setProductSKU("productSKU");
        productInfoInRequest.setQuantity(1);
        productInfoInRequest.setUnitPrice(price);
        productInfo.add(productInfoInRequest);
        orderInfo.setProductInfo(productInfo);
        request.setOrderInfo(orderInfo);

        // 账单信息
        BillToInfoInRequest billToInfo = new BillToInfoInRequest();
        billToInfo.setAddress1("1 Market St");
        billToInfo.setAdministrativeArea("CA");
        billToInfo.setCountry("US");
        billToInfo.setEmail("test@test.com");
        billToInfo.setFirstName("John");
        billToInfo.setLastName("Doe");
        billToInfo.setPostalCode("48104-2201");
        billToInfo.setLocality("san francisco");
        request.setBillToInfo(billToInfo);


        // 配送信息
        ShipToInfoInRequest shipToInfo = new ShipToInfoInRequest();
        shipToInfo.setCity("san francisco");
        shipToInfo.setCountry("US");
        shipToInfo.setFirstName("John");
        shipToInfo.setLastName("Doe");
        shipToInfo.setPhoneNumber("85267461399");
        shipToInfo.setPostalCode("48104-2201");
        shipToInfo.setState("CA");
        shipToInfo.setStreet1("1 Market St");
        request.setShipToInfo(shipToInfo);

        // 设备信息
        DeviceInfoInRequest deviceInfo = new DeviceInfoInRequest();
        deviceInfo.setBrowserLanguage("en-US");
        deviceInfo.setIpAddress("127.0.0.1");
        request.setDeviceInfo(deviceInfo);

        PaymentsPayResponse response = service.pay(request);
        System.out.println(JSONObject.toJSONString(response));
    }
}
